package m3;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * 消息消费方
 *
 * @author 郭鲁政
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.64.140");
        connectionFactory.setPassword("admin");
        connectionFactory.setUsername("admin");
        connectionFactory.setPort(5672);
        Channel channel = connectionFactory.newConnection().createChannel();
        String s = UUID.randomUUID().toString();



        channel.queueDeclare(s, true, false, false, null);
        channel.exchangeDeclare("test-3", BuiltinExchangeType.FANOUT);
        channel.queueBind(s, "test-3", "");
        DeliverCallback deliverCallback = (String consumerTag, Delivery message) -> {
            byte[] body = message.getBody();
            System.out.println("接受到消息"+new String(body));
            for (char c : new String(body).toCharArray()) {
                if (c=='.'){
                    System.out.println("线程等待");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        };
        CancelCallback cancelCallback =(String consumerTag)-> {


        };
        channel.basicConsume(s, true,deliverCallback,cancelCallback );
    }
}
